﻿@model IEnumerable<Common.Product>
           
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>View Products</h2>
<script type="text/javascript">
    function addToCart(id) {
        var regex_num = new RegExp('^[1-9]$', "");

        if ($("#quantity" + id + "").val() == "") {
            alert("Input quantity to buy!");
        }
        else if ($("#quantity" + id + "").val() == "0") {
            alert("Quantity should be more than 0!");
        }
        else if (isNaN($("#quantity" + id + "").val())) {
            alert("Only digits are accepted");
        }
        else if (!regex_num.test($("#quantity" + id + "").val())) {
            alert("Only digits are accepted");
        }
        else {
            var param = {
                pId: id,
                quantity: $('#quantity' + id + '').val()
            };
            $.ajax({
                url: '@Url.Action("AddToCart")',
                type: "POST",
                datatype: "json",
                data: param,
                error: function (xmlHttpRequest, errorText, thrownError) {
                    alert(xmlHttpRequest + "|" + errorText + "|" + thrownError);
                },
                success: function (data) {
                    var msg = data.hasOwnProperty("not enough stock") ? data.d : data;
                    if (data != null) {
                        if (data == "not enough stock") {
                            alert("Not enough product stock! Try a lower quantity");
                            $('#quantity' + id + '').val("");
                        }
                        else {
                            alert("Product has been successfully added to your cart!");
                            $('#quantity' + id + '').val("");
                        }
                    }
                }
            });
        }
    }   
</script>

<table>
    <tr>
        <th>Product Name</th>
        <th>Features</th>
        <th>Image</th>
        <th>Stock</th>
        <th>Price</th>
        <th>Date Listed</th>
        <th>Comments</th>
        <th>View Comments</th>
        <th>Average Rating</th>
        <th>Purchase</th>
    </tr>

@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.ProductName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Features)
        </td>
        <td>
            <img height="150px" width="150px" src= "@Url.Content(item.ImageLink)" alt="@Html.DisplayFor(modelItem => item.ImageLink)" />
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.StockQty)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Price)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DateListed)
        </td>
        <td>
            <div id='@Html.DisplayFor(modelItem => item.ProductID)'></div>
        </td>
        <td>
            @Ajax.ActionLink("View Comments", "GetCommentsByProductID", new { productID = item.ProductID },
              new AjaxOptions
              {
                  HttpMethod = "GET",
                  UpdateTargetId = item.ProductID.ToString(),
                  InsertionMode = InsertionMode.Replace
              })
        </td>
        <td> 
             @Html.Action("ReturnAverageRating", "Products", new { productID = item.ProductID })
        </td>
        <td>
            @if (item.StockQty != 0)
            {
                @Html.Label("Quantity: ")
                @Html.TextBox("quantity" + @item.ProductID + "")
                <button type="button" onclick="addToCart(@item.ProductID)">Add to Cart</button>
            }
            else
            {
                <span style="color:Red">
                    @Html.Label("Product is out of stock")
                </span>
            }
        </td>
    </tr>
}
</table>
